package cn.bytecollege.user.controller;

import cn.bytecollege.common.entity.User;
import cn.bytecollege.user.service.UserService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 检查用户名是否被注册
 */
@WebServlet(name = "UsernameCheckController",value = "checkUsername")
public class UsernameCheckController extends HttpServlet {
    private UserService userService = null;
    public UsernameCheckController() {
        userService = new UserService();
    }
        @Override
        protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
        {
            doPost(request,response);
        }

        @Override
        protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            response.setContentType("text/html;charset=utf-8");
            String msg = null;
            String username = request.getParameter("username");
            User user = userService.findUserByUsername(username);
            if(user!=null){
                //说明该用户名已经被注册
                msg = "该用户名已经被注册！请更换";
                response.getWriter().write(msg);
                return;
            }
            msg = "该用户名可以注册";
            response.getWriter().write(msg);
        }
    }

